package com.example.szh.mvp.view.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.szh.R;
import com.example.szh.adapter.ContentNewsAdapter;
import com.example.szh.adapter.ImageBannerAdapter;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.ImageBannerEntity;
import com.example.szh.entity.NewsEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.mvp.di.DaggerLREConponent;
import com.example.szh.mvp.di.LREMoudes;
import com.example.szh.mvp.presenter.LREPresenter;
import com.example.szh.nextwork.view.BaseVpFragment;
import com.example.szh.utils.ToastUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContentNewsFragment extends BaseVpFragment<LREPresenter> implements LREContract.ILREView
        ,OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener {

    private long channel_id;
    private SmartRefreshLayout content_news_smart;
    private RecyclerView content_news_rv;
    private View headView;
    private ViewPager headVp;
    private ImageBannerAdapter vpAdapter;
    private ContentNewsAdapter adapter;

    public ContentNewsFragment(long channel_id){
        this.channel_id = channel_id;
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof ImageBannerEntity){
            //banner列表返回
            ImageBannerEntity bannerEntity = (ImageBannerEntity) entity;
            vpAdapter = new ImageBannerAdapter(bannerEntity.getValues());
            headVp.setAdapter(vpAdapter);

        }
        if (entity instanceof NewsEntity){
            //新闻列表返回
            NewsEntity newsEntity = (NewsEntity) entity;
            adapter.setNewData(newsEntity.getValues());
        }
    }



    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        NewsEntity newsEntity = (NewsEntity) entity;
        adapter.setNewData(newsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        NewsEntity newsEntity = (NewsEntity) entity;
        if (newsEntity.getValues()==null){
            ToastUtils.showMsg("最后一页");
            return;
        }
        if (newsEntity.getValues().size() == 0){
            ToastUtils.showMsg("最后一页");
            return;
        }
        List<NewsEntity.Values> list = adapter.getData();
        list.addAll(newsEntity.getValues());
        adapter.setNewData(list);
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_content_news;
    }

    @Override
    public void initView() {
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_news_banner,null);
        headVp = headView.findViewById(R.id.news_banner_vp);
        content_news_smart = f(R.id.content_news_smart);
        content_news_rv = f(R.id.content_news_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        content_news_rv.setLayoutManager(manager);
        adapter=new ContentNewsAdapter();
        adapter.addHeaderView(headView);
        content_news_rv.setAdapter(adapter);
        content_news_smart.setRefreshHeader(new ClassicsHeader(getActivity()));
        content_news_smart.setRefreshFooter(new ClassicsFooter(getActivity()));
        content_news_smart.setOnRefreshLoadMoreListener(this);
        adapter.setOnItemClickListener(this);
        headVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                vpAdapter.getImg().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        NewsEntity.Values values = (NewsEntity.Values) adapter.getItem(position);
                        String url = values.news_url;
                        long id =values.news_id;
                        Bundle bundle = new Bundle();
                        bundle.putLong("news_id",id);
                        bundle.putString("news_url",url);
                        startPage(bundle,NewsValuesActivity.class);
                    }
                });
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void inject() {
        DaggerLREConponent.builder().lREMoudes(new LREMoudes(this)).build().injectContentNewsFragment(this);
    }

    @Override
    public void initData() {
//根据创建当前fragment对象时的channel_id获取对应新闻数据
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        Map<String,Object>bannerMap = new HashMap<>();
        bannerMap.put("code",HttpCode.IMAGERBANNERCODE);
        p.requestAll(bannerMap,map);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        Map<String,Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id",1);
        p.requestRefresh(map);
        refreshLayout.finishRefresh();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        NewsEntity.Values values = (NewsEntity.Values) adapter.getItem(position);
        String url = values.news_url;
        long id =values.news_id;
        Bundle bundle = new Bundle();
        bundle.putLong("news_id",id);
        bundle.putString("news_url",url);
        startPage(bundle,NewsValuesActivity.class);
    }
}
